package com.doxiaopu.students.organization.controller;

import com.doxiaopu.students.component.controller.BaseController;
import com.doxiaopu.students.component.result.ResponseResult;
import com.doxiaopu.students.organization.entity.Organization;
import com.doxiaopu.students.organization.service.IOrganizationService;
import com.doxiaopu.students.organization.vo.OrganizationVO;
import com.doxiaopu.students.product.vo.SingleVO;
import com.doxiaopu.students.user.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@Api(tags = "组织信息")
@RestController
@CrossOrigin
@RequestMapping("/organization")
public class OrganizationController extends BaseController {
    @Autowired
    private IOrganizationService iOrganizationService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    //创建社联
    @ApiOperation(value = "创建社联")
    @PostMapping("/createCouncil")
    @ResponseBody
    public ResponseResult<Organization> createCouncil( Organization organization , HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        if(id !=null) {
            iOrganizationService.createCouncil(organization,id);
            return new ResponseResult<Organization>(SUCCESS,"保存成功");
        }else {
            return new ResponseResult<Organization>(408,"登录过期请重新登录");
        }
    }

    @ApiOperation(value = "获得后端社联列表")
    @PostMapping("/getCouncilList")
    @ResponseBody
    public ResponseResult<Map<String, Object>> getouncilList(OrganizationVO organizationVO , HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        if(id !=null) {
            Map<String, Object> data = iOrganizationService.getOrganizationsList(organizationVO);
            return new ResponseResult<Map<String, Object>>(SUCCESS,data);
        }else {
            return new ResponseResult<Map<String, Object>>(408,"登录过期请重新登录");
        }
    }

    @ApiOperation(value = "获得前端社联列表")
    @PostMapping("/getAppCouncilList")
    @ResponseBody
    public ResponseResult<List<Organization>> getAppCouncilList(OrganizationVO organizationVO , HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        if(id !=null) {
            List<Organization>  data = iOrganizationService.getAPPOrganizations(organizationVO);
            return new ResponseResult<List<Organization> >(SUCCESS,data);
        }else {
            return new ResponseResult<List<Organization> >(408,"登录过期请重新登录");
        }
    }

    @ApiOperation(value = "获得前端协会列表")
    @PostMapping("/getAppAssociationList")
    @ResponseBody
    public ResponseResult<List<Organization>> getAppAssociationList(OrganizationVO organizationVO , HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        if(id !=null) {
            List<Organization>  data = iOrganizationService.getAPPCommunitytionss(organizationVO);
            return new ResponseResult<List<Organization> >(SUCCESS,data);
        }else {
            return new ResponseResult<List<Organization> >(408,"登录过期请重新登录");
        }
    }


    //社团申请
    @ApiOperation(value = "社团申请")
    @PostMapping("/applyAssociation")
    @ResponseBody
    public ResponseResult<Organization> applyAssociation( Organization organization , HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        if(id !=null) {
            iOrganizationService.createAssociation(organization,id);
            return new ResponseResult<Organization>(SUCCESS,"保存成功");
        }else {
            return new ResponseResult<Organization>(408,"登录过期请重新登录");
        }
    }

    @ApiOperation(value = "获得协会列表")
    @PostMapping("/getCommunitytionsList")
    @ResponseBody
    public ResponseResult<Map<String, Object>> getCommunitytionsList(OrganizationVO organizationVO , HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        if(id !=null) {
            Map<String, Object> data = iOrganizationService.getCommunitytionsList(organizationVO,id);
            return new ResponseResult<Map<String, Object>>(SUCCESS,data);
        }else {
            return new ResponseResult<Map<String, Object>>(408,"登录过期请重新登录");
        }
    }

    @ApiOperation(value = "修改协会信息")
    @PostMapping("/updateCommunitytions")
    @ResponseBody
    public ResponseResult<Void> updateCommunitytions(Organization organization , HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        if(id !=null) {
             iOrganizationService.updateApply(organization,id);
            return new ResponseResult<Void>(SUCCESS,"更改成功");
        }else {
            return new ResponseResult<Void>(408,"登录过期请重新登录");
        }
    }
    //查询团单详情的协会列表
    @ApiOperation(value = "查询团单详情的协会列表")
    @PostMapping("/getAssociationList")
    @ResponseBody
    public ResponseResult<List<Organization>> getAssociationList( HttpServletRequest request){
        Integer id =getUidFromRequest(request);
        if(id !=null) {
            List<Organization>  data = iOrganizationService.getSingleCommunitytionss();
            return new ResponseResult<List<Organization> >(SUCCESS,data);
        }else {
            return new ResponseResult<List<Organization> >(408,"登录过期请重新登录");
        }
    }

    private Integer getUidFromRequest(HttpServletRequest request){
        //获取headers中的参数
        String token = request.getHeader("token");
        if(stringRedisTemplate.opsForValue().get(token) != null) {
            return Integer.valueOf(stringRedisTemplate.opsForValue().get(token).toString());
        }else {
            return null;
        }
    }
}
